<?php
// +----------------------------------------------------------------------
// | Author: 林子彦
// +----------------------------------------------------------------------
namespace Lapi\Model;
use Think\Model;
/**
 * 会员模型
 */
class MembersModel extends Model{
	
    protected $_auto = array(
		//array('birthday', 'strtotime', self::MODEL_BOTH, 'function'),
                //array('logo', 'setLogoUrl', self::MODEL_BOTH, 'callback'),
		array('reg_ip', 'get_client_ip', self::MODEL_INSERT, 'function', 1),
                array('reg_time', NOW_TIME, self::MODEL_INSERT),
		array('status', 'getStatus', self::MODEL_BOTH, 'callback'),
		array('update_time',NOW_TIME, 3)        
        
     );
    
    /**
     * 用户登陆
     */
    public function login($username,$password,$type){

    	$map = array();
    	switch ($type) {
    		case 1:
    			$map['username'] = $username;
    			break;
    		case 2:
    			$map['email'] = $username;
    			break;
    		case 3:
    			$map['mobile'] = $username;
    			break;
    		case 4:
    			$map['id'] = $username;
    			break;
    		default:
    			return 0; //参数错误
    	}
    	
    	/* 获取用户数据 */
    	$user = $this->where($map)->find();
    	
    	if(is_array($user) && $user['status']){
 
    		/* 验证用户密码 */
    		if(lqw_ucenter_md5($password, C('DATA_AUTH_KEY')) === $user['password']){
    			return $user['uid']; //登录成功，返回用户ID
    		} else {
    			return -2; //密码错误
    		}
    	} else {
    		return -1; //用户不存在或被禁用
    	}
    } 
    
    
	/**
	 * 获取用户信息
	 * @param  string  $uid         用户ID或用户名
	 * @param  boolean $is_username 是否使用用户名查询
	 * @return array                用户信息
	 */
	public function info($uid, $is_username = false){
		$map = array();
		if($is_username){ //通过用户名获取
			$map['username'] = $uid;
		} else {
			$map['uid'] = $uid;
		}

		$user = $this->field(true)->where($map)->find();
		return $user;
	}
        /**
         * 保存数据
         */
        public function saveData($data){
           return $this->save($data);
        }
 /**
         * 保存数据
         */
        public function addData($data){
           return $this->add($data);
        }
	

}
